﻿using System;
using System.IO;
using System.Reflection;
using System.Threading;

using libPlugin;

namespace tinterBot
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = string.Format("tinterBot v{0}.{1} (b{2}.{3})", ver.Major, ver.Minor, ver.Build, ver.Revision);

            ConWrite("\r\nMilestone 1 Solutions\r\n");
            ConWrite(string.Format("TinterBot v{0}.{1} (b{2}.{3})\r\n\r\n", ver.Major, ver.Minor, ver.Build, ver.Revision));
            LogWrite("\r\nMilestone 1 Solutions\r\n");
            LogWrite(string.Format("TinterBot v{0}.{1} (b{2}.{3})\r\n\r\n", ver.Major, ver.Minor, ver.Build, ver.Revision));

            foreach (IBotPlugin plugin in pluginManager.pluginList)
            {
                ConWrite(string.Format("Plugin \"{0}\" Loaded.\r\n", plugin.Name));
            }

            networkManager.netThread.Start();
            jobScheduler.jobThread.Start();

            while (networkManager.netThread.ThreadState != System.Threading.ThreadState.Stopped)
            {
                if (Console.ReadKey().Key == ConsoleKey.Escape)
                {
                    break;
                }
            }

            //Shutdown

            jobScheduler.jobThread.Abort();

            foreach (Thread thread in pluginManager.threadList)
            {
                if (thread.ThreadState == ThreadState.WaitSleepJoin)
                    thread.Interrupt();
                thread.Abort();
            }

            networkManager.netClient.Close();
            logStream.Close();
            conStream.Close();
        }

        public static Version ver = Assembly.GetEntryAssembly().GetName().Version;

        public static StreamWriter conStream = new StreamWriter(Directory.GetCurrentDirectory() + "\\console.txt", true);
        public static StreamWriter logStream = new StreamWriter(Directory.GetCurrentDirectory() + "\\log.txt", true);

        public static void ConWrite(string text)
        {
            Console.Write(text);
            conStream.Write(text);
        }

        public static void LogWrite(string text)
        {
            logStream.Write(text);
        }
    }
}
